/*
 * NewOkCancelDialog.java
 *
 * Created on 14 de junio de 2008, 16:12
 */
package presentacion.vista;

import accesoDatos.dao.DaoEmpleado;
import accesoDatos.dao.DaoProducto;
import accesoDatos.vo.Empleado;
import accesoDatos.vo.Producto;
import javax.swing.JOptionPane;
import presentacion.util.Ventana;

/**
 *
 * @author  Tatis
 */
public class NewOkCancelDialog extends javax.swing.JDialog {

    /** A return status code - returned if Cancel button has been pressed */
    public static final int RET_CANCEL = 0;
    /** A return status code - returned if OK button has been pressed */
    public static final int RET_OK = 1;
    private String idp;
    private int verificar;
    private int id2 = 0;

    /** Creates new form NewOkCancelDialog */
    public NewOkCancelDialog(java.awt.Frame parent, boolean modal, String id, int i) {
        super(parent, modal);
        initComponents();
        Ventana.centrar(this);
        idp = id;
        id2 = Integer.parseInt(idp);
        verificar = i;
        if (verificar == 0) {
            Producto p = DaoProducto.read(id2);
            mensaje.setText("Está seguro que desea borrar el producto:");
            nombre.setText(p.getNombre() + "?");
        }
        if (verificar == 1) {
            Empleado e = DaoEmpleado.read(id2);
            mensaje.setText("Está seguro que desea borrar al empleado:");
            nombre.setText(e.getNombre() + "?");

        }
    }

    /** @return the return status of this dialog - one of RET_OK or RET_CANCEL */
    public int getReturnStatus() {
        return returnStatus;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        mensaje = new javax.swing.JLabel();
        nombre = new javax.swing.JLabel();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        mensaje.setFont(new java.awt.Font("Tahoma", 2, 14));
        mensaje.setText("Está seguro que desea borrarlo?");

        nombre.setFont(new java.awt.Font("Tahoma", 2, 14));
        nombre.setText("jLabel1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(152, 152, 152)
                .addComponent(nombre)
                .addContainerGap(185, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(116, 116, 116)
                        .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(cancelButton))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(83, 83, 83)
                        .addComponent(mensaje, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGap(100, 100, 100))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cancelButton, okButton});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(22, Short.MAX_VALUE)
                .addComponent(mensaje)
                .addGap(18, 18, 18)
                .addComponent(nombre)
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(okButton)
                    .addComponent(cancelButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        if (verificar == 0) {
            Producto p = DaoProducto.read(id2);
            if (p.getEstaEnMenu() == true) {
                JOptionPane.showMessageDialog(this, "No se puede borrar " + p.getNombre() + " porque esta asignada a algun pedido");
                doClose(RET_OK);
            } else {
                DaoProducto.delete(p);
                p.setEstaEnMenu(false);
                JOptionPane.showMessageDialog(this, "Se borró exitosamente " + p.getNombre());
                doClose(RET_OK);
            }

        }
        if (verificar == 1) {
            Empleado e = DaoEmpleado.read(id2);
            if (e.getCargo().equals("Administrador")) {
                JOptionPane.showMessageDialog(this, "El administrador no puede ser eliminado");
                doClose(RET_OK);
            } else {
                DaoEmpleado.delete(e);
                JOptionPane.showMessageDialog(this, "Se borró exitosamente " + e.getNombre());
                doClose(RET_OK);

            }

        }
    }//GEN-LAST:event_okButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        doClose(RET_CANCEL);
    }//GEN-LAST:event_cancelButtonActionPerformed

    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        doClose(RET_CANCEL);
    }//GEN-LAST:event_closeDialog

    private void doClose(int retStatus) {
        returnStatus = retStatus;
        setVisible(false);
        dispose();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                NewOkCancelDialog dialog = new NewOkCancelDialog(new javax.swing.JFrame(), true, new String(), 2);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel mensaje;
    private javax.swing.JLabel nombre;
    private javax.swing.JButton okButton;
    // End of variables declaration//GEN-END:variables
    private int returnStatus = RET_CANCEL;
}
